package com.pic.mgt.monitor.parser;

import java.io.File;

import org.apache.commons.lang.StringUtils;

import com.pic.mgt.monitor.WrongUnitFormatException;

public final class MessageTransactionFileParser extends FormatParser {
	private static String messageConfirmationFilenameEndWith;

	@Override
	/**
	 * 分析判斷對應的 Transaction File 是否存在，若存在則會回傳對應的 Transaction File。
	 */
	public File parse(Object _confirmationFile) throws WrongUnitFormatException {
		// 確認是否為 File 型態
		// throws WrongUnitFormatException
		super.checkObjectType(_confirmationFile, File.class);
		
		File confirmationFile = (File)_confirmationFile;
		
		File transactionFile = getTransactionFile(confirmationFile);

		if (!transactionFile.exists()) {
			/*
			 *  注意!!! 如果要修改訊息拋出內容要小心!! 因為會有其它類別依照此訊
			 *  息內容來作為一些判斷的準則。
			 */
			throw new WrongUnitFormatException(
			"Transaction file doesn't exist.");
		}

		return transactionFile;
	}
	
	public static File getTransactionFile(File confirmationFileForMsg) {
		String transactionFileStr = StringUtils.removeEnd(
								confirmationFileForMsg.getAbsolutePath(),
								messageConfirmationFilenameEndWith);		
		return new File(transactionFileStr);
	}
	
	// --- getter and setter ---
	
	public void setMessageConfirmationFilenameEndWith(String msgConfirmationFilenameEndWith) {
		messageConfirmationFilenameEndWith = msgConfirmationFilenameEndWith;
	}
}
